package com.sharekitchen;

import com.sun.deploy.net.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class 高德地图API测试 {

    @Test
    public void test1(){
        try {
            // 替换成你的高德地图API Key
//            String apiKey = "07030115396a7f18744a1836e9e2b8d7";
//            String apiKey = "a1da374e8f6b019ec155741304641f99";
            String apiKey = "6c5b817b24bce82746c3c53c270c3892";
            // 地址
            String address = "广东省广州市番禺区";
            // 构建URL
            String url = "https://restapi.amap.com/v3/geocode/geo?key="+apiKey+"&address="+address+"&city=广州";
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet request = new HttpGet(url);
            CloseableHttpResponse response = httpClient.execute(request);
            // 获取响应内容
            String result = EntityUtils.toString(response.getEntity());
            System.out.println(result);
            String longitude = result.split("location")[1].split("\"")[2].split(",")[0];
            String latitude = result.split("location")[1].split("\"")[2].split(",")[0];
            System.out.println(longitude+latitude);
            // 这里你需要解析返回的JSON以获取经纬度信息
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2(){
        String str = "{\"status\":\"1\",\"info\":\"OK\",\"infocode\":\"10000\",\"count\":\"1\",\"geocodes\":[{\"formatted_address\":\"广东省广州市番禺区\",\"country\":\"中国\",\"province\":\"广东省\",\"citycode\":\"020\",\"city\":\"广州市\",\"district\":\"番禺区\",\"township\":[],\"neighborhood\":{\"name\":[],\"type\":[]},\"building\":{\"name\":[],\"type\":[]},\"adcode\":\"440113\",\"street\":[],\"number\":[],\"location\":\"113.383917,22.937560\",\"level\":\"区县\"}]}";
        String latitude = str.split("location")[1].split("\"")[2];
        System.out.println(latitude);

    }
}
